#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int maxn = nums.size() - 1;
        int ret = 0;
        while (maxn >= 2)
        {
            int left = 0, right = maxn - 1;
            while (right > left)
            {
                if (nums[left] + nums[right] > nums[maxn])
                {
                    ret += right - left;
                    right--;
                }
                else
                {
                    left++;
                }
            }
            maxn--;
        }
        return ret;
    }
};